package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.AddFriend;
import com.ruoyi.system.domain.MemberPackageConfig;
import com.ruoyi.system.domain.TUser;
import com.ruoyi.system.domain.vo.AddFriendSearchVo;
import com.ruoyi.system.domain.vo.AddFriendVo;
import com.ruoyi.system.dto.AddFriendDTO;
import com.ruoyi.system.mapper.AddFriendMapper;
import com.ruoyi.system.mapper.MemberPackageConfigMapper;
import com.ruoyi.system.service.IAddFriendService;
import com.ruoyi.system.service.ITUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddFriendServiceImpl extends ServiceImpl<AddFriendMapper, AddFriend> implements IAddFriendService {
    @Autowired
    private ITUserService tUserService;
    @Override
    public Boolean addOrUpdateByFriend(AddFriendVo vo) {
        // 查询用户信息
        TUser user = tUserService.selectTUserByUid(vo.getOwnerId());
        AddFriend addFriend = new AddFriend();
        BeanUtils.copyProperties(vo,addFriend);
        addFriend.setPhone(user.getMobile());
        addFriend.setUserName(user.getName());
        addFriend.setOwnerId(user.getUid());
        if (vo.getId() != null){
            return baseMapper.updateById(addFriend)>0;
        }
        return baseMapper.insert(addFriend)>0;
    }

    @Override
    public List<AddFriendDTO> getAddFriendList(AddFriendSearchVo vo) {
        LambdaQueryWrapper<AddFriend> wrapper = new LambdaQueryWrapper<>();
        // 关键字模糊搜索
        wrapper.like(StringUtils.isNotBlank(vo.getKeyword()),
                AddFriend::getUserName, vo.getKeyword())
            .or(StringUtils.isNotBlank(vo.getKeyword()),
                w -> w.like(AddFriend::getPhone, vo.getKeyword()));
        // 时间区间
        wrapper.le(vo.getStartTime() != null, AddFriend::getStartTime, vo.getStartTime());
        wrapper.ge(vo.getEndTime()   != null, AddFriend::getEndTime,   vo.getEndTime());
        wrapper.orderByDesc(AddFriend::getCreateTime);
        List<AddFriend> addFriends = baseMapper.selectList(wrapper);
        List<AddFriendDTO> dtos = new ArrayList<>();
        for (AddFriend addFriend : addFriends) {
            AddFriendDTO dto = new AddFriendDTO();
            BeanUtils.copyProperties(addFriend,dto);
            LocalDate endTime = addFriend.getEndTime();
            // 包含当前日期
            if (endTime != null && (endTime.isAfter(LocalDate.now()) || endTime.isEqual(LocalDate.now()))) {
                dto.setIsExpired(1);
            } else {
                dto.setIsExpired(0);
            }
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public int countList(AddFriendSearchVo vo) {
        LambdaQueryWrapper<AddFriend> wrapper = new LambdaQueryWrapper<>();
        // 关键字模糊搜索
        wrapper.like(StringUtils.isNotBlank(vo.getKeyword()),
                AddFriend::getUserName, vo.getKeyword())
            .or(StringUtils.isNotBlank(vo.getKeyword()),
                w -> w.like(AddFriend::getPhone, vo.getKeyword()));
        // 时间区间
        wrapper.ge(vo.getStartTime() != null, AddFriend::getStartTime, vo.getStartTime());
        wrapper.le(vo.getEndTime()   != null, AddFriend::getEndTime,   vo.getEndTime());
        return Math.toIntExact(baseMapper.selectCount(wrapper));
    }

    @Override
    public Boolean addFriendCheck(String userId) {
        List<AddFriend> addFriends = baseMapper.selectList(new LambdaQueryWrapper<AddFriend>().eq(AddFriend::getOwnerId, userId));
        // 判断是否存在未过期的加好友权限
        for (AddFriend addFriend : addFriends) {
            LocalDate endTime = addFriend.getEndTime();
            if (endTime != null && (endTime.isAfter(LocalDate.now()) || endTime.isEqual(LocalDate.now()))) {
                return true;
            }
        }
        return false;
    }
}
